/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.b3log.nide.model.source.java;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class defines all Java method model relevant keys.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.2, Aug 3, 2010
 */
public final class JavaMethod {

    /**
     * Indicates method modifiers.
     */
    public static final String METHOD_MODIFIERS = "methodModifiers";
    /**
     * Indicates method return type.
     */
    public static final String METHOD_RETURN_TYPE = "methodReturnType";
    /**
     * Indicates method name.
     */
    public static final String METHOD_NAME = "methodName";
    /**
     * Indicates method parameter types.
     */
    public static final String METHOD_PARAMETER_TYPES = "methodParameterTypes";
    /**
     * Indicates method exception types.
     */
    public static final String METHOD_EXCEPTION_TYPES = "methodExceptionTypes";

    /**
     * Transforms the specified method into a json object.
     *
     * @param method the specified method
     * @return a json object depicts a method, for example:
     * <pre>
     * {
     *     "methodModifiers": "public static final",
     *     "methodReturnType": "java.lang.String",
     *     "methodName": "sayGreeting",
     *     "methodParameterTypes": [
     *         "java.lang.String", "boolean"
     *     ],
     *     "methodExceptionTypes": [
     *         "java.lang.Exception"
     *     ]
     * }
     * </pre>
     * @throws JSONException json exception
     */
    public static JSONObject transform(final Method method)
            throws JSONException {
        final JSONObject ret = new JSONObject();

        final String modifiers = Modifier.toString(method.getModifiers());
        final String returnType = method.getReturnType().getCanonicalName();
        final String name = method.getName();
        final JSONArray parameterTypes = new JSONArray();
        for (final Class<?> parameterType : method.getParameterTypes()) {
            parameterTypes.put(parameterType.getCanonicalName());
        }
        final JSONArray exceptionTypes = new JSONArray();
        for (final Class<?> exceptionType : method.getExceptionTypes()) {
            exceptionTypes.put(exceptionType.getCanonicalName());
        }

        ret.put(METHOD_MODIFIERS, modifiers);
        ret.put(METHOD_RETURN_TYPE, returnType);
        ret.put(METHOD_NAME, name);
        ret.put(METHOD_PARAMETER_TYPES, parameterTypes);
        ret.put(METHOD_EXCEPTION_TYPES, exceptionTypes);

        return ret;
    }

    /**
     * Private default constructor.
     */
    private JavaMethod() {
    }
}
